Skip to content

[feature] Convert count on not null column to count(0) - #91

Merged
litianfeng1998 merged 6 commits into
OpenTenBase:8.0.30from
litianfeng1998:issue549-count-rewrite
Jun 5, 2026
Merged

litianfeng1998 merged 6 commits into
OpenTenBase:8.0.30from
litianfeng1998:issue549-count-rewrite

Conversation

@litianfeng1998

Copy link
Copy Markdown
Collaborator

What

Convert count(col) to count(0) when the column is guaranteed NOT NULL (e.g. a NOT NULL column not on the inner side of an outer join). In that case count(col) is equivalent to count(0), and the per-row column processing overhead can be saved when the column is not referenced elsewhere.

Controlled by a new session sysvar txsql_count_conversion_enabled (default ON). Two new status vars expose the effect: Txsql_total_count_function and Txsql_implicit_convert_to_count_star.

Commits

  • [feature] Convert count on not null column to count(0) -- per-field reference count (TABLE::fields_use_count) + count(0) conversion during optimization.
  • [bugfix] postfix -- use real_sum_func() in ExplainAccessPath so Item_rollup_sum_switcher is handled correctly.
  • [bugfix] count rewrite asan mem leak postfix -- clear fields_use_count after query finishes.
  • [bugfix] cnt rule asan leak postfix -- store fields_use_count as a Mem_root_array<uint> allocated from the TABLE mem_root.
  • [port] adapt for OpenTenBase/TXSQL 8.0.30 -- indentation fix and regenerated mtr result.

Testing

  • Full build passes with gcc-toolset-10 in both RelWithDebInfo and Debug configurations.
  • New mtr test main.txsql_convert_to_count_star passes (both --record and normal runs) on a debug server.

Problem:
=======
If a counted column is guaranteed NOT NULL (e.g. a NOT NULL column not on
the inner side of an outer join), count(col) is equivalent to count(0),
and the column processing overhead can be saved when it is not referenced
elsewhere.

Solution:
=======
Add a per-field reference count (TABLE::fields_use_count) and clear the
read_set bit of fields no longer referenced; convert count(not_null_col)
to count(0) during optimization.

New sysvar:  txsql_count_conversion_enabled, default ON
New statvar: Txsql_implicit_convert_to_count_star, Txsql_total_count_function
Use real_sum_func() instead of sum_func() when deciding whether to print
the count-conversion info in ExplainAccessPath, so that special items such
as Item_rollup_sum_switcher (whose sum_func() returns the child aggregate
type) do not cause an incorrect cast/print.
The count conversion feature added TABLE::fields_use_count. Since MySQL
TABLE objects are recycled without explicitly calling the destructor, the
vector did not release its memory. Explicitly clear fields_use_count for
every open table after the query finishes.
The count conversion feature introduced a std::vector member in TABLE which
is not allocated from mem_root; since TABLE is managed by the mem_root
mechanism and its destructor is not called, the vector leaked. Replace
fields_use_count with a Mem_root_array<uint> allocated from the TABLE
mem_root so it is compatible with MySQL memory management.
- table.cc: brace the PART_KEY_FLAG / merge_keys branches so the added
  use_field() calls are correctly guarded (fixes -Werror=misleading-
  indentation under debug build with gcc-toolset-10).
- regenerate mysql-test/r/txsql_convert_to_count_star.result against this
  repo via mtr --record (test passes in both record and normal runs).
- drop the .pq result variant carried over from the source repo; it is
  environment-specific and not validated here (default .result is used).
@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@litianfeng1998

Copy link
Copy Markdown
Collaborator Author

@copilot resolve the merge conflicts in this pull request

@litianfeng1998
litianfeng1998 merged commit bd0a30d into OpenTenBase:8.0.30 Jun 5, 2026
1 of 2 checks passed
flyfox20 pushed a commit that referenced this pull request Jun 23, 2026
flyfox20 pushed a commit that referenced this pull request Jun 23, 2026
* [feature] Convert count on not null column to count(0)

Problem:
=======
If a counted column is guaranteed NOT NULL (e.g. a NOT NULL column not on
the inner side of an outer join), count(col) is equivalent to count(0),
and the column processing overhead can be saved when it is not referenced
elsewhere.

Solution:
=======
Add a per-field reference count (TABLE::fields_use_count) and clear the
read_set bit of fields no longer referenced; convert count(not_null_col)
to count(0) during optimization.

New sysvar:  txsql_count_conversion_enabled, default ON
New statvar: Txsql_implicit_convert_to_count_star, Txsql_total_count_function

* [bugfix] Convert count on not null column to count(0) postfix

Use real_sum_func() instead of sum_func() when deciding whether to print
the count-conversion info in ExplainAccessPath, so that special items such
as Item_rollup_sum_switcher (whose sum_func() returns the child aggregate
type) do not cause an incorrect cast/print.

* [bugfix] count rewrite asan mem leak postfix

The count conversion feature added TABLE::fields_use_count. Since MySQL
TABLE objects are recycled without explicitly calling the destructor, the
vector did not release its memory. Explicitly clear fields_use_count for
every open table after the query finishes.

* [bugfix] cnt rule asan leak postfix

The count conversion feature introduced a std::vector member in TABLE which
is not allocated from mem_root; since TABLE is managed by the mem_root
mechanism and its destructor is not called, the vector leaked. Replace
fields_use_count with a Mem_root_array<uint> allocated from the TABLE
mem_root so it is compatible with MySQL memory management.

* [port] adapt count conversion for OpenTenBase/TXSQL 8.0.30

- table.cc: brace the PART_KEY_FLAG / merge_keys branches so the added
  use_field() calls are correctly guarded (fixes -Werror=misleading-
  indentation under debug build with gcc-toolset-10).
- regenerate mysql-test/r/txsql_convert_to_count_star.result against this
  repo via mtr --record (test passes in both record and normal runs).
- drop the .pq result variant carried over from the source repo; it is
  environment-specific and not validated here (default .result is used).

---------

Co-authored-by: litianfeng1998 <litianfeng1998@users.noreply.github.com>
flyfox20 pushed a commit that referenced this pull request Jun 24, 2026
* [feature] Convert count on not null column to count(0)

Problem:
=======
If a counted column is guaranteed NOT NULL (e.g. a NOT NULL column not on
the inner side of an outer join), count(col) is equivalent to count(0),
and the column processing overhead can be saved when it is not referenced
elsewhere.

Solution:
=======
Add a per-field reference count (TABLE::fields_use_count) and clear the
read_set bit of fields no longer referenced; convert count(not_null_col)
to count(0) during optimization.

New sysvar:  txsql_count_conversion_enabled, default ON
New statvar: Txsql_implicit_convert_to_count_star, Txsql_total_count_function

* [bugfix] Convert count on not null column to count(0) postfix

Use real_sum_func() instead of sum_func() when deciding whether to print
the count-conversion info in ExplainAccessPath, so that special items such
as Item_rollup_sum_switcher (whose sum_func() returns the child aggregate
type) do not cause an incorrect cast/print.

* [bugfix] count rewrite asan mem leak postfix

The count conversion feature added TABLE::fields_use_count. Since MySQL
TABLE objects are recycled without explicitly calling the destructor, the
vector did not release its memory. Explicitly clear fields_use_count for
every open table after the query finishes.

* [bugfix] cnt rule asan leak postfix

The count conversion feature introduced a std::vector member in TABLE which
is not allocated from mem_root; since TABLE is managed by the mem_root
mechanism and its destructor is not called, the vector leaked. Replace
fields_use_count with a Mem_root_array<uint> allocated from the TABLE
mem_root so it is compatible with MySQL memory management.

* [port] adapt count conversion for OpenTenBase/TXSQL 8.0.30

- table.cc: brace the PART_KEY_FLAG / merge_keys branches so the added
  use_field() calls are correctly guarded (fixes -Werror=misleading-
  indentation under debug build with gcc-toolset-10).
- regenerate mysql-test/r/txsql_convert_to_count_star.result against this
  repo via mtr --record (test passes in both record and normal runs).
- drop the .pq result variant carried over from the source repo; it is
  environment-specific and not validated here (default .result is used).

---------

Co-authored-by: litianfeng1998 <litianfeng1998@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants